//富文本解析器
var WxParse = require('../../wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        descr:'',
        music:'',
        image:'',
        width:'0%',
        end:'00.00',
        start:'00.00',

        buttonstatus:true
    },
    stop(e){
        
        this.innerAudioContext.pause(()=>{})
        this.setData({
            buttonstatus: true
        })
    },
    play(e) {
        var that = this
        this.setData({
            buttonstatus: false
        })
        this.innerAudioContext.play(() => {
            
        })
        setTimeout(() => {
            this.innerAudioContext.onTimeUpdate(() => {
                //console.log(this.innerAudioContext.duration)  //总时长
                //console.log(this.innerAudioContext.currentTime)  //当前播放进度
                let sumtime = this.innerAudioContext.duration;
                let newtime = this.innerAudioContext.currentTime;
                let width = newtime / sumtime * 100 + '%';

                // 总时间
                let sumtimem = parseInt(sumtime/60);
                let sumtimes = parseInt(sumtime%60);
                let newtimem = parseInt(newtime / 60);
                let newtimes = parseInt(newtime % 60);
                
                let end = sumtimem + ':' + sumtimes;
                let start = newtimem + ':' + newtimes
                console.log(end)
                console.log(start)
                that.setData({
                    width: width,
                    end:end,
                    start:start,
                })
            })
        }, 500)
        
        //  自然播放完后
        this.innerAudioContext.onEnded(() => {
          that.setData({
            width: '0%',
            end: '00.00',
            start: '00.00',

            buttonstatus: true
          })
        })
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this
        console.log(options)
         wx.getStorage({
           key: 'yejt',
           success: function(res) {
             console.log(res)
             that.setData({
               image:res.data.image,
               music:res.data.music,
               descr:res.data.descr
             })
             if (res.data.descr) {
               WxParse.wxParse('article', 'html', res.data.descr, that, 5);
             }
           },
         })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.showLoading({
            title: '正在缓冲',
        })
        var that = this
        this.innerAudioContext = wx.createInnerAudioContext()
        this.innerAudioContext.autoplay = true
        this.innerAudioContext.src = this.data.music
        this.innerAudioContext.onError((res) => {
            console.log(res.errMsg)
            console.log(res.errCode)
        })
        setTimeout(() => {
            this.innerAudioContext.onTimeUpdate(() => {
                console.log(this.innerAudioContext.duration)  //总时长
                console.log(this.innerAudioContext.currentTime)  //当前播放进度
                let sumtime = this.innerAudioContext.duration;
                let newtime = this.innerAudioContext.currentTime
                let width = newtime / sumtime * 100 + '%'
                    console.log(width)
                // 总时间
                let sumtimem = parseInt(sumtime / 60);
                let sumtimes = parseInt(sumtime % 60);
                if (sumtimem<10){
                    sumtimem = "0" + sumtimem
                }
                if (sumtimes < 10) {
                    sumtimes = "0" + sumtimes
                }
                let newtimem = parseInt(newtime / 60);
                let newtimes = parseInt(newtime % 60);


                if (newtimem < 10) {
                  newtimem = "0" + newtimem
                }
                if (newtimes < 10) {
                  newtimes = "0" + newtimes
                }

                let end = sumtimem + ':' + sumtimes;
                let start = newtimem + ':' + newtimes
                console.log(end)
                console.log(start)
                that.setData({
                    width: width,
                    end: end,
                    start: start,
                })
            })
        }, 500)

        this.innerAudioContext.onPlay(() => {
            wx:wx.hideLoading()
            that.setData({
                buttonstatus:false
            })
        })
         
        //  自然播放完后
         this.innerAudioContext.onEnded(()=>{
           that.setData({
             width: '0%',
             end: '00.00',
             start: '00.00',

             buttonstatus: true
           })
         })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.innerAudioContext.pause(() => {

        })
        // this.setData({
        //     buttonstatus: true
        // })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})